package middleware.redis;

import java.util.concurrent.TimeUnit;

import com.me.common.util.SleepUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;

/**
 * 
 *
 * @author ME
 * @date   2020/6/4
 */
public class TestRedisson {

    public static void main(String[] args) throws InterruptedException {
        // redisson配置
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://127.0.0.1:6379");
        // singleServerConfig.setPassword("redis");
        // redisson客户端
        RedissonClient redissonClient = RedisUtils.getInstance().getRedisson(config);
        RBucket<Object> rBucket = RedisUtils.getInstance().getRBucket(redissonClient, "key");
        // rBucket.set("wangnian");
        System.out.println("RBucket: " + rBucket.get());

        RLock lock = redissonClient.getLock("lock");
        // 由于 Redis锁 使用完之后会主动释放，所以 每10秒的延时任务 只需要判断锁是否还在（Hash 中是否有指定键），锁在则继续续期

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                System.out.println("time: " + i);
                SleepUtil.seconds(1);
            }
        }).start();

        new Thread(() -> {
            System.out.println("thread-1 run...");
            try {
                // 在 10s 内尝试获取锁，超过则 返回false
                // lock.tryLock(10, TimeUnit.SECONDS);

                // 第一个参数代表等待时间，第二是代表超过时间释放锁，第三个代表设置的时间制
                boolean tryLock = lock.tryLock(2, 20, TimeUnit.SECONDS);

                if (tryLock) {
                    try {
                        System.out.println("thread-1 加锁成功，执行了 15秒。。。");
                        Thread.sleep(15000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread-1 stop...");
        }, "thread-1").start();

        SleepUtil.ms(100);
        new Thread(() -> {
            System.out.println("thread-2 start...");
            try {
                // 在 10s 内尝试获取锁，超过则 返回false
                boolean tryLock = lock.tryLock(5, TimeUnit.SECONDS);

                if (tryLock) {
                    try {
                        System.out.println("thread-2 加锁成功，执行了 10秒。。。");
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("thread-2 stop...");
        }, "thread-2").start();

    }
}
